﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid;
using WoSoft.PACS.Data.CheckData;
using DevComponents.DotNetBar;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmPatientSelect : FrmBase
    {

        /// <summary>
        /// 查询开始时间
        /// </summary>
        public DateTime StartTime { get; private set; }
        public DateTime EndTime { get; private set; }
        public string ApplyDepartmentID { get; private set; }
        public string PatientName { get; private set; }
        public string PatientID { get; private set; }
        public string ExeDepartmentID { get; private set; }
        public int HasApp { get; private set; }
        public int HasBD { get; private set; }
        public int Executeing { get; private set; }
        public int Finish { get; private set; }
        public string Jcxmmc { get; private set; }

        public string AppCheckID { get; set; }//创建的申请ID
        public string AppCheckType { get; set; }//检查类型

        //检查的业务对象
        private CheckApp _checkapp = new CheckApp();

        public FrmPatientSelect()
        {
            InitializeComponent();
        }

        private void FrmCheckAppSearch_Load(object sender, EventArgs e)
        {
            //日期数据
            this.dtStartTime.Value = Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            this.dtEndTime.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).AddDays(1).AddSeconds(-1);

            //加载部门
            this.LoadDepartment(this.cbApplyDepartment);
            this.LoadDepartment(this.cbExeDepartment);

            this.cbExeDepartment.SelectedValue = Program.CurrentLoginUser.DepartmentID;

            btnOK_Click(null, null);
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.StartTime = this.dtStartTime.Value;
            this.EndTime = this.dtEndTime.Value;
            this.ApplyDepartmentID = this.cbApplyDepartment.SelectedValue != null ? this.cbExeDepartment.SelectedValue.ToString() : "";
            this.PatientID = this.tb_PatientID.Text.Trim();
            this.PatientName = this.tb_PatientName.Text.Trim();

            this.ExeDepartmentID = this.cbExeDepartment.SelectedValue != null ? this.cbExeDepartment.SelectedValue.ToString() : "";
            this.HasApp = this.ckApp.Checked ? 1 : 0;
            this.HasBD = this.ckBD.Checked ? 1 : 0;
            this.Executeing = this.ckExe.Checked ? 1 : 0;
            this.Finish = this.ckFinish.Checked ? 1 : 0;
            this.Jcxmmc = this.tb_jcxmmc.Text.Trim();

            //执行查询
            var dt = this._checkapp.GetCheckListResult(StartTime, EndTime, ApplyDepartmentID, PatientID, PatientName, ExeDepartmentID, HasApp, HasBD, Executeing, Finish, Jcxmmc);
            this.superGridControl.PrimaryGrid.DataSource = dt;
            this.superGridControl.PrimaryGrid.GroupByRow.Text = "状态";

            this.superGridControl.PrimaryGrid.AutoExpandSetGroup = true;
            this.superGridControl.PrimaryGrid.GridPanel.AddGroup(this.superGridControl.PrimaryGrid.Columns["gridColstate"]);
        }

        //关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void superGridControl_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {
            var rows = this.superGridControl.PrimaryGrid.GetSelectedRows();
            if (rows == null || rows.Count == 0)
            {
                return;
            }
            var row = rows[0];
            GridRow gridRow = row as GridRow;
            if (gridRow != null)
            {
                this.AppCheckID = gridRow["gridColID"].Value.ToString();
                this.AppCheckType = gridRow["gridColCheckType"].Value != null ? gridRow["gridColCheckType"].Value.ToString() : string.Empty;
                //state_Report = gridRow["gridColreportState"].Value.ToString();//报告文档状态
                //state_ID = gridRow["gridColstate"].Value.ToString();//状态 1=已申请/2=已报到/3=报告中/4=已完成

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var rows = this.superGridControl.PrimaryGrid.GetSelectedRows();
            if (rows == null || rows.Count == 0)
            {
                return;
            }
            var row = rows[0];
            GridRow gridRow = row as GridRow;
            if (gridRow != null)
            {
                DialogResult dialogResult = MessageBoxEx.Show("确认删除该病人报告信息？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No) return;
                this.AppCheckID = gridRow["gridColID"].Value.ToString();

                _checkapp.DeleteAll(this.AppCheckID);
                btnOK_Click(null, null);
            }
        }
    }
}
